package InterfaceCirculoMohr;

import CirculoMohr.CirculoDeMohr;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.math.BigDecimal;

/**
 *
 * @author Fabricio
 */
public class PanelCirculoMohr extends javax.swing.JPanel {

    private CirculoDeMohr circulo;

    private int raio;
    private int iMin;

    /** Creates new form PanelCirculoMohr */
    public PanelCirculoMohr(CirculoDeMohr circulo) {

        this.initComponents();

        this.circulo = circulo;
        iMin = (int) (300 * (circulo.getInerciaMinima()) / (circulo.getInerciaMinima() + 2 * circulo.getRaio()));
        raio = (int) (300 * (circulo.getRaio()) / (circulo.getInerciaMinima() + 2 * circulo.getRaio()));
        this.repaint();

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (circulo != null) {
            Graphics2D g2 = (Graphics2D) g;

            //Seta configurações basicas de exibição das linhas
            g.setColor(Color.black);
            g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));

            //Eixo Y
            g.drawLine(20, 5, 20, 355);
            g.drawString("P", 25, 25);
            Polygon p = new Polygon();
            p.addPoint(20 - 5, 5);
            p.addPoint(20 + 5, 5);
            p.addPoint(20, 0);
            g.fillPolygon(p);

            //Eixo X
            g.drawLine(5, 180, 345, 180);
            g.drawString("I", 335, 180 - 10);
            p = new Polygon();
            p.addPoint(340, 180 - 5);
            p.addPoint(340, 180 + 5);
            p.addPoint(345, 180);
            g.fillPolygon(p);

            //Centro da circunferência
            float tracos[] = {10};
            g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, tracos, 0));
            g.drawLine(25 + iMin + raio, 5, 25 + iMin + raio, 355);
            g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));

            //Circulo
            g.drawOval(iMin + 25, 180 - raio, raio * 2, raio * 2);

            //Angulo
            if(circulo.getAnguloGraus() != 0){
                g.drawLine(25 + iMin + raio, 180, 25 + iMin + raio + (int) (Math.cos(circulo.getAngulo()) * raio), 180 - (int) (Math.sin(circulo.getAngulo()) * raio));
                g.drawArc(
                        25 + iMin + raio - (int)raio/4,
                        180 - (int)raio/4,
                        (int)raio/2,
                        (int)raio/2,
                        0,
                        (int) circulo.getAnguloGraus());
                g.drawString(
                        " " + new BigDecimal(circulo.getAnguloGraus()).setScale(2, BigDecimal.ROUND_HALF_UP) + "º",
                        30 + iMin + raio + (int) (Math.cos(circulo.getAngulo()) * raio),
                        170 - (int) (Math.sin(circulo.getAngulo()) * raio));
            }

            //Barras
            this.paintHorizontalLine(g, 20, 400, iMin + 25, 400, "Imin = " + new BigDecimal(circulo.getInerciaMinima()).setScale(2, BigDecimal.ROUND_HALF_UP));
            this.paintHorizontalLine(g, 20, 435, iMin + 25 + raio, 435, "Imed = " + new BigDecimal(circulo.getInerciaMedia()).setScale(2, BigDecimal.ROUND_HALF_UP));
            this.paintHorizontalLine(g, 20, 470, iMin + 25 + (raio * 2), 470, "Imax = " + new BigDecimal(circulo.getInerciaMaxima()).setScale(2, BigDecimal.ROUND_HALF_UP));

            //Eixo Linha
            g.setColor(Color.red);
            g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
            g.drawLine(
                    25 + iMin + raio - (int) (raio * Math.cos(circulo.getAnguloEixo())),
                    180 + (int) (raio * Math.sin(circulo.getAnguloEixo())),
                    25 + iMin + raio + (int) (raio * Math.cos(circulo.getAnguloEixo())),
                    180 - (int) (raio * Math.sin(circulo.getAnguloEixo())));
            g.drawArc(
                    25 + iMin + raio - (int)raio/5,
                    180 - (int)raio/5,
                    (int)2*raio/5,
                    (int)2*raio/5,
                    0,
                    (int) (circulo.getAnguloEixoGraus()));
            g.drawString(" " + new BigDecimal(circulo.getAnguloEixoGraus()).setScale(2, BigDecimal.ROUND_HALF_UP) + "º", 30 + iMin + raio + (int) (Math.cos(circulo.getAnguloEixo()) * raio),
                    170 - (int) (Math.sin(circulo.getAnguloEixo()) * raio));

        }
    }

    private void paintHorizontalLine(Graphics g, int x1, int y1, int x2, int y2, String legenda) {
        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x1, y1 - 5, x1, y1 + 5);
        g.drawLine(x2, y2 - 5, x2, y2 + 5);
        g.drawString(legenda, x1, y1 - 10);
    }





    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables


}
